from math import sqrt


def integral_rec_down (f, i, step):
    return step * f(i)

def integral_rec_up (f, i, step):
    return step * f(i + step)

def integral_trapeze (f, i, step):
    return step * f(i) + (f(i+step) - f(i)) * step / 2.

def integral_middle (f, i, step):
    return step * f(i + step / 2.)

def integral_simpson (f, i, step):
    return step * (f(i)/6. + 2*f(i+step/2.)/3. + f(i+step)/6.)

def integral_boole_villarceau (f, i, step):
    return step * (7*f(i)/90. + 16*f(i+step/4.)/45. + 2*f(i+2*step/4.)/15. + 16*f(i+3*step/4.)/45. + 7*f(i+step)/90.)



def integral(f, a, b, g, epsi=0.01):
    "Fonction calculant l'integrale"
    integ = 0
    integ_save = epsi + 1
    subdiv = 2
    while (abs(integ - integ_save) > epsi):
        integ_save = integ
        integ = 0
        step = (b - a) / float(subdiv)
        i = a
        while i < b:
            integ += g(f, i, step)
            i += step
        subdiv *= 2

    return integ



def splineLength(fp, a, b, int_method):
    "Fonction determinant la longueur de courbe"
    f_ = lambda x: sqrt(1 + fp(x)**2)
    return integral(f_, a, b, int_method)